package com.jpc.musicdelightapplication.ui.square.detail

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.jpc.lib_base.utils.LogUtil
import com.jpc.musicdelightapplication.R
import com.jpc.musicdelightapplication.base.BaseActivity
import com.jpc.musicdelightapplication.databinding.ActivityPlaylistDetailBinding

private const val TAG = "PlaylistDetailActivity"
class PlaylistDetailActivity : BaseActivity<PlaylistDetailViewModel, ActivityPlaylistDetailBinding>(R.layout.activity_playlist_detail) {
    companion object{
        const val PLAYLIST_ID = "playlist_id"
        fun launch(context: Context, playlistId: Long){
            context.startActivity(Intent(context, PlaylistDetailActivity::class.java).apply {
                putExtra(PLAYLIST_ID, playlistId)
            })
        }
    }
    private var playlistId: Long = 0L
    private val mAdapter by lazy { ItemPlaylistSongAdapter() }
    override fun initView(savedInstanceState: Bundle?) {
        // 得到歌单的id
        playlistId = intent.getLongExtra(PLAYLIST_ID, 0L)
        mBinding.apply {
            showLoading = true
            recyclerView.apply {
                layoutManager = LinearLayoutManager(this@PlaylistDetailActivity)
                adapter = mAdapter

            }
        }

        mViewModel.fetchPlaylistAndSongList(playlistId, false)
    }


    @SuppressLint("NotifyDataSetChanged")
    override fun createObserve() {
        super.createObserve()
        mViewModel.playlistLiveData.observe(this@PlaylistDetailActivity){ playlist ->
            mBinding.showLoading = false
            mAdapter.notifyDataSetChanged()
            LogUtil.d(TAG, playlist?.name ?: "no")
            mBinding.tvName.text = playlist?.name
        }
//        mViewModel.songListLiveData.observe(this@PlaylistDetailActivity){
//            mBinding.showLoading = false
//            mAdapter.notifyDataSetChanged()
//        }
    }
}